package types

import (
	"sync"
	"sync/atomic"

	"gitee.com/cjianwen/go/socks/v2"
)

var instance *socks.Serializer
var once sync.Once
var typeId atomic.Int32

func getInstance() *socks.Serializer {

	once.Do(func() {
		instance = socks.NewSerializer(true, 1023)
	})
	return instance
}

const (
	TypeNone = iota
	TypeEntryData
)

func RegisterType[T any]() {
	id := typeId.Add(1)
	getInstance().RegisterType(id, (*T)(nil))
}

func Encode(a any) ([]byte, bool) {
	return getInstance().Encode(a)
}

func Decode(bits []byte, a any) bool {
	return getInstance().Decode(bits, a)
}
